package com.woniu.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 邮件消费者，处理 邮件消息
 *
 * */
@Component
@Slf4j
public class EmailListener {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 监听指定队列，并将消息注入方法参数
     * */
    @RabbitListener(queues = "email.queue")
    public void receiveEmail(Map<String,String> emailMsg) {
        log.info("监听到队列email.queue的消息，开始发送邮件，消息数据：{}", emailMsg);
        // 获取邮件信息
        String from = emailMsg.get("from");
        String to = emailMsg.get("to");
        String subject = emailMsg.get("subject");
        String content = emailMsg.get("content");
        // 发送邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        javaMailSender.send(simpleMailMessage);
    }

    void test() {
        // DirectExchange全参构造
        TopicExchange topicExchange = new TopicExchange("topicExchange", true, false);


    }
}
